﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using MethodWorx.CMS.Providers;
using MethodWorx.CMS.TemplateLanguage;
using MethodWorx.CMS.TemplateLanguage.Design;
using MethodWorx.CMS.TemplateLanguage.Properties;

namespace MethodWorx.CMS.Plugins.GlobalProperties
{
    public class GlobalPropertyParser : IParser, IDesignable, IMetaParser
    {
        internal static GlobalPropertiesExtension Plugin { get; set; }

        [DependencyProperty]
        public ICmsDataProvider Provider { get; set; }

        [Property(Alias = "Name", Description="Global property name")]
        public string PropertyName { get; set; }

        public object Parse(Web.CmsRequestContext context, IEnumerable<object> stack, Dictionary<string, object> parameters)
        {


            return this.GetGlobalProperty();
        }

        private HtmlResult GetGlobalProperty()
        {
            if (!string.IsNullOrEmpty(PropertyName))
            {
                PluginService service = new PluginService(this.Provider);

                var list = service.GetAllUserData<object>(Plugin, null, "GlobalProperty");
                PropertyDetails<object> propertyDetails;

                foreach (var item in list)
                {
                    propertyDetails =
                        (PropertyDetails<object>)
                        item.UserData.GetType().InvokeMember("Cast",
                                                    BindingFlags.InvokeMethod | BindingFlags.Public |
                                                    BindingFlags.Instance,
                                                    null, item.UserData, new object[] { typeof(object) });

                    if (propertyDetails != null && propertyDetails.Name.ToLower() == PropertyName.ToLower())
                    {
                        return new HtmlResult()
                        {
                            Content = propertyDetails.Custom
                        };
                    }
                }
            }

            return null;
        }

        public string Name
        {
            get { return "Property"; }
        }

        public string Namespace
        {
            get { return "Global"; }
        }

        public void Design(IDesigner designer)
        {
            this.Provider = designer.GetProvider();
            var htmlresult = this.GetGlobalProperty();
            if (htmlresult != null)
            {
                designer
                .AddControl("Global:Property")
                .AddContentBeforeChildren("<span>" + htmlresult.Content + "</span>");
            }
            else
            {
                designer
                .AddControl("Global:Property")
                .AddContentBeforeChildren("<i>" + this.PropertyName + "</i>");
            }

        }

        public string GetDescription()
        {
            return "Outputs / Selects the value of a global propery";
        }

        public ParserMetaParameter[] GetParameters(Stack<object> stack, ParserMetaParameter[] parameters)
        {
            return parameters;
        }
    }
}
